package xujiwen.transport.Mgr;

import java.util.ArrayList;
import java.util.List;

import xujiwen.transport.bean.SDDis;
import xujiwen.transport.util.DBConnect;

public class SDDisMgr {
	public void Save(final SDDis sddis) {
		final DBConnect db = new DBConnect();
		final String sql = "insert into SDIS values (null, ?, ?, ?)";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setString(1, sddis.getEntryName());
			db.setString(2, sddis.getExitName());
			db.setDouble(3, sddis.getDis());
			db.excute();
			db.commit();

		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	
	
	public void Update(final SDDis sddis) {
		final DBConnect db = new DBConnect();
		final String sql = "update SDIS set EntryName=?, ExitName=?, DIS=? where SDID=?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setString(1, sddis.getEntryName());
			db.setString(2, sddis.getExitName());
			db.setDouble(3, sddis.getDis());
			db.setInt(4, sddis.getSdid());
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	
	public Double GetDis(String s1, String s2) {
		Double dis = 0.0;
		final DBConnect db = new DBConnect();
		final String sql= "select DIS from SDIS where EntryName=? and ExitName=?";
		//System.out.println(sql);
		db.prepareStatement(sql);
		db.setString(1, s1);
		db.setString(2, s2);
		db.excuteQuery();
		try {
			while (db.next()) {
				dis = db.getDouble("DIS");
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return dis;
	}
	
	public List<SDDis> GetSearchDisList(String str) {
		final List<SDDis> users = new ArrayList<SDDis>();
		final DBConnect db = new DBConnect();
		final String sql;
		if(str.length() > 0)
		sql = "select * from SDIS where EntryName like '%" + str +"%' or ExitName like '%" + str +"%'";
		else sql = "select * from SDIS limit 10";
		//System.out.println(sql);
		db.prepareStatement(sql);
		db.excuteQuery();
		try {
			while (db.next()) {
				final SDDis sddis = new SDDis();
				sddis.setSdid(db.getInt("SDID"));
				sddis.setEntryName(db.getString("EntryName"));
				sddis.setExitName(db.getString("ExitName"));
				sddis.setDis(db.getDouble("DIS"));
				
				users.add(sddis);
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return users;
	}
	public void Del(final int sdid)
	{
		final DBConnect db = new DBConnect();
		final String sql = "delete from SDIS where SDID = ?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setInt(1, sdid);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
}
